package org.van.blog.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.van.blog.constant.LogActions;
import org.van.blog.constant.Types;
import org.van.blog.controller.BaseController;
import org.van.blog.dto.cond.ContentCond;
import org.van.blog.dto.cond.MetaCond;
import org.van.blog.model.ContentDomain;
import org.van.blog.model.MetaDomain;
import org.van.blog.service.article.ContentService;
import org.van.blog.service.log.LogService;
import org.van.blog.service.meta.MetaService;
import org.van.blog.utils.APIResponse;

@Controller
@RequestMapping("/admin/article")
public class ArticleController extends BaseController {

    @Autowired
    private MetaService metaService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private LogService logService;

    @GetMapping(value = "")
    public String index(
            HttpServletRequest request,
            @RequestParam(name = "page", required = false, defaultValue = "1") int page,
            @RequestParam(name = "limit", required = false, defaultValue = "15") int limit) {
        PageInfo<ContentDomain> articles = contentService.getArticlesByCond(new ContentCond(), page, limit);
        request.setAttribute("articles", articles);
        return "admin/article_list";
    }

    @GetMapping(value = "/publish")
    public String newArticle(HttpServletRequest request) {
        MetaCond metaCond = new MetaCond();
        metaCond.setType(Types.CATEGORY.getType());
        List<MetaDomain> metas = metaService.getMetas(metaCond);
        request.setAttribute("categories", metas);
        return "admin/article_edit";
    }

    @GetMapping(value = "/{cid}")
    public String editArticle(
            @PathVariable Integer cid,
            HttpServletRequest request) {
        ContentDomain content = contentService.getArticleById(cid);
        request.setAttribute("contents", content);
        MetaCond metaCond = new MetaCond();
        metaCond.setType(Types.CATEGORY.getType());
        List<MetaDomain> categories = metaService.getMetas(metaCond);
        request.setAttribute("categories", categories);
        request.setAttribute("active", "article");
        return "admin/article_edit";
    }

    @PostMapping("/modify")
    @ResponseBody
    public APIResponse<?> modifyArticle(
            HttpServletRequest request,
            @RequestParam(name = "cid", required = true) Integer cid,
            @RequestParam(name = "title", required = true) String title,
            @RequestParam(name = "titlePic", required = false) String titlePic,
            @RequestParam(name = "slug", required = false) String slug,
            @RequestParam(name = "content", required = true) String content,
            @RequestParam(name = "type", required = true) String type,
            @RequestParam(name = "status", required = true) String status,
            @RequestParam(name = "tags", required = false) String tags,
            @RequestParam(name = "categories", required = false, defaultValue = "Default") String categories,
            @RequestParam(name = "allowComment", required = true) Boolean allowComment) {
        ContentDomain contentDomain = new ContentDomain();
        contentDomain.setTitle(title);
        contentDomain.setCid(cid);
        contentDomain.setTitlePic(titlePic);
        contentDomain.setSlug(slug);
        contentDomain.setContent(content);
        contentDomain.setType(type);
        contentDomain.setStatus(status);
        contentDomain.setTags(tags);
        contentDomain.setCategories(categories);
        contentDomain.setAllowComment(allowComment ? 1 : 0);
        contentService.updateArticleById(contentDomain);

        return APIResponse.success();
    }

    @PostMapping(value = "/publish")
    @ResponseBody
    public APIResponse<?> publishArticle(
            @RequestParam(name = "title", required = true) String title,
            @RequestParam(name = "titlePic", required = false) String titlePic,
            @RequestParam(name = "slug", required = false) String slug,
            @RequestParam(name = "content", required = true) String content,
            @RequestParam(name = "type", required = true) String type,
            @RequestParam(name = "status", required = true) String status,
            @RequestParam(name = "categories", required = false, defaultValue = "Default") String categories,
            @RequestParam(name = "tags", required = false) String tags,
            @RequestParam(name = "allowComment", required = true) Boolean allowComment) {
        ContentDomain contentDomain = new ContentDomain();
        contentDomain.setTitle(title);
        contentDomain.setTitlePic(titlePic);
        contentDomain.setSlug(slug);
        contentDomain.setContent(content);
        contentDomain.setType(type);
        contentDomain.setStatus(status);
        contentDomain.setHits(1);
        contentDomain.setCommentsNum(0);
        contentDomain.setTags(type.equals(Types.ARTICLE.getType()) ? tags : null);
        contentDomain.setCategories(type.equals(Types.ARTICLE.getType()) ? categories : null);
        contentDomain.setAllowComment(allowComment ? 1 : 0);
        contentService.addArticle(contentDomain);
        return APIResponse.success();
    }

    @PostMapping("/delete")
    @ResponseBody
    public APIResponse<?> deleteArticle(
            @RequestParam(name = "cid", required = true) Integer cid,
            HttpServletRequest request) {
        contentService.deleteArticleById(cid);
        logService.addLog(LogActions.DEL_ARTICLE.getAction(), cid + "", request.getRemoteAddr(), this.getUid(request));
        return APIResponse.success();
    }

}
